package cn.com.wxd.action.admin;

import javax.servlet.http.HttpServletRequest;

import cn.com.wxd.action.ValidatorResult;
import cn.com.wxd.common.CharUtil;

/**
 * 用户模块功能的验证
 * Created by ComXu on 2017/5/19.
 */
class AdminCtlValidator extends ValidatorResult {

     static ValidatorResult loginValidator(HttpServletRequest request, String userName, String passWord,
                                                 String verCode) {
        if (CharUtil.isEmpty(userName)) {
            return failuerValidatorResult("用户名不能为空!");
        }
        if (userName.length() < 6) {
            return failuerValidatorResult("用户名长度不能少于6!");
        }
        if (passWord == null || passWord.trim().equals("")) {
            return failuerValidatorResult("密码不能为空!");
        }
        if (verCode == null || verCode.trim().equals("")) {
            return failuerValidatorResult("验证码不能为空!");
        }
        String verCodesession = (String) request.getSession().getAttribute("vercode");
        request.getSession().removeAttribute("vercode");  //获取后立马移除
        if (verCodesession == null) {
            verCodesession = "";
        }
        if (!verCodesession.equalsIgnoreCase(verCode.trim())) {
            return failuerValidatorResult("验证码错误!");
        }
        return ValidatorResult.successValidatorResult();
    }

    static ValidatorResult applyforValidator(HttpServletRequest request, String userName, String password,
                                                  String rePassword,
                                                  String verCode) {

        if (CharUtil.isEmpty(userName)) {
            return failuerValidatorResult("用户名不能为空！");
        }
        if (userName.length() < 6) {
            return failuerValidatorResult("用户名长度不能小于6！");
        }
        if (userName.length() != CharUtil.stringLength(userName)) {
            return failuerValidatorResult("用户名不能含有中文符号！");
        }
        if (Character.isDigit(userName.charAt(0))) {
            return failuerValidatorResult("用户名不能以数字开头！");
        }
        if (CharUtil.isEmpty(password)) {
            return failuerValidatorResult("密码不能为空！");
        }
        if (!password.equals(rePassword)) {
            return failuerValidatorResult("两次密码不一致！");
        }

        if (CharUtil.isEmpty(verCode)) {
            return failuerValidatorResult("验证码不能为空！");
        }
        String verCodesession = (String) request.getSession().getAttribute("vercode");
        request.getSession().removeAttribute("vercode");  //获取后立马移除
        if (verCodesession == null) {
            verCodesession = "";
        }
        if (!verCodesession.equalsIgnoreCase(verCode.trim())) {
            return failuerValidatorResult("验证码不正确！");
        }
        return successValidatorResult();
    }
}